import {
  rootPath, API, wsPath
} from "../../utils/const";
import {
  formatTime
} from "../../utils/dateUtils";
import {
  getRequest,
  requestUserInfo
} from "../../utils/request"
import {
  getUserInfo
} from "../../utils/storageUtils";
// pages/home.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    deptName: '',
    userInfo: {},
    usingCount: 0,
    watiMeCount: 0,
    disinfectCount: 0,
    iconList: []
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    requestUserInfo();

    const userRole = wx.getStorageSync('loginType');
    let iconList = [];
    if (userRole === 'doctor') {
      iconList = [
        {
          icon: '/public/我的档案.png', page: '/pages/sendMaterial/sendMaterial', text: '术后资料推送'
        },
        {
          icon: '/public/我的档案.png', page: '/pages/chatList/chatlist', text: '病人反馈'
        }
      ]
    } else if (userRole === 'patient') {
      iconList = [
        { 
          icon: '/public/inboxin-fill.png', page: '/pages/getMaterials/getMaterials', text: '术后资料'
        },
        {
          icon: '/public/inboxin-fill.png', page: '/pages/chatList/chatlist', text: '咨询医生'
        },
        {
          icon: '/public/inboxin-fill.png', page: '/pages/submitFeedback/submitFeedback', text: '术后反馈'
        }
      ]
    }

    this.setData({
      iconList: iconList
    });
  },

  handleIconClick: function(e) {
    const page = e.currentTarget.dataset.page;
    wx.navigateTo({
      url: page
    });
  },

  loadTime() {
    // this.setIn
    const i = setInterval(() => {

      const dateStr = formatTime(Date.now(), "YY") + "年" + formatTime(Date.now(), "MM") + "月" + formatTime(Date.now(), "DD") + "日";
      const weekStr = formatTime(Date.now(), "Week");
      const timeStr = formatTime(Date.now(), "hh-mm-ss");
      // console.log("current", dateStr, weekStr, timeStr, Date.now())
      this.setData({
        dateStr,
        weekStr,
        timeStr
      })
    }, 1000)
    this.setData({
      it: i
    })

  },
  fetchUsingCount: function () {
    getRequest(API.equipUsingCount).then(res => {
      this.setData({
        usingCount: res.count
      })
    })
  },
  fetchConfirmCount: function (params) {
    {
      getRequest(API.waitConfirmGroupCount, params).then(res => {
        this.setData({
          watiMeCount: res.count
        })
      })
    }
  },
  fetchDisfinctCount: function (params) {
    {
      getRequest(API.waitConfirmGroupCount, params).then(res => {
        this.setData({
          disinfectCount: res.count
        })
      })
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.loadTime();
    const userInfo = getUserInfo();
    this.fetchUsingCount();
    this.fetchDisfinctCount({
      equipStatus: 'DISINFECTING'
    });
    this.fetchConfirmCount({
      equipStatus: 'RECYCLE_CONFIRM'
    });
    this.setData({
      userInfo,
    })
  },
  onSquireItemClick: function (e) {

    const {
      path
    } = e.currentTarget.dataset;
    wx.navigateTo({
      url: path,
    })
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {
    clearInterval(this.data.it)
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {
    console.log("unload")
    // webSocketManager.unregisterCallback('NotificationRecord', this.handleWsMessage);
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})